package com.example.garbage.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.garbage.pojo.*;
import com.example.garbage.service.TruckService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Shunrai
 * @create 2024/3/16 15:55
 * @Version 1.0
 * @Description
 */
@RestController
@ApiResponse(responseCode = "404", description = "查询失败")
@ApiResponse(responseCode = "200", description = "查询成功")
@RequestMapping("/trunk")
@Tag(name = "垃圾运输车")
public class TrunckController {
    @Autowired
    private TruckService truckService;

    @GetMapping("/getbydepot/{did}")
    @Operation(summary = "获取垃圾运输车通过出发点")
    public RespBean<Truck> getAllGSite(@PathVariable("did")  Integer did) {
        QueryWrapper wrapper;

            wrapper= new QueryWrapper<Truck>().eq("did", did);

        List<Truck> list = truckService.list(wrapper);
        return RespBean.success("查询成功", list);
    }

    @GetMapping("/getall")
    @Operation(summary = "获取垃圾运输车")
    public RespBean<TruckDTO> getAllGSite1(@RequestParam(value = "current",required = false)@Parameter(description = "不带任何参数返回全部结果")Integer current, @RequestParam(value = "PAGE_SIZE",required = false)Integer PAGE_SIZE,
                                        @RequestParam(value = "keyword",required = false)@Parameter(description = "默认为'',搜索关键词") String keyword) {
        Wrapper<Truck> address;
        if(current==null||"".equals(current)){
            current=1;
        }
        if (keyword==null||"".equals(keyword) ) {
            keyword = "%";
            address= new QueryWrapper<Truck>().like("carnumber", keyword);
        }else {
            address = new QueryWrapper<Truck>().like("carnumber", keyword);
        }

        if(PAGE_SIZE==null||"".equals(PAGE_SIZE)){
            PAGE_SIZE= Math.toIntExact(truckService.count());
        }
        Page<Truck> personalEBikePage = new Page<>(current, PAGE_SIZE);
        Page<Truck> page = truckService.page(personalEBikePage,address);

        TruckDTO siteDTO = new TruckDTO(page.getRecords(), (int) page.getTotal());
        return RespBean.success("查询成功", siteDTO);
    }
}
